﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using ES.Shared.Data;

namespace ES.Shared.Deployment
{
    public class DatabaseSetupContext : IDisposable
    {
        private volatile SqlConnection _connection;

        public DatabaseSetupContext(string serverName, string databaseName)
        {
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder.DataSource=serverName;
            builder.InitialCatalog=databaseName;
            builder.IntegratedSecurity=true;
            builder.ApplicationName = "entrastructure";

            _connection = new SqlConnection(builder.ToString());
            _databaseTables = new TableInfoCollection();
        }

        public DatabaseSetupContext(string serverName, string databaseName, string sqlUser, string sqlPassword)
        {
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder.DataSource = serverName;
            builder.InitialCatalog = databaseName;
            builder.IntegratedSecurity = false;
            builder.UserID = sqlUser;
            builder.Password = sqlPassword;
            builder.ApplicationName = "entrastructure";

            _connection = new SqlConnection(builder.ToString());
            _databaseTables = new TableInfoCollection();
        }

        private TableInfoCollection _databaseTables;

        public TableInfoCollection DatabaseTables
        {
            get { return _databaseTables; }
        }

        public void SynchronizeDatabase()
        {
            throw new NotImplementedException();
        }

        #region IDisposable Member

        private bool _disposing = false;

        public void Dispose()
        {
            if (_disposing)
                return;

            _disposing = true;

            if (_connection != null)
            {
                _connection.Dispose();
                _connection = null;
            }
        }

        #endregion
    }
}
